#!/usr/bin/env python
# -*- coding: utf-8 -*-

# thirdpart modules
from PyQt5.QtCore import (
    QSize,
    QStringListModel,
    Qt,
)

from PyQt5.QtGui import (
    QCursor,
    QIcon,
)

from PyQt5.QtWidgets import (
    QAbstractItemView,
    QDockWidget,
    QListView,
    QMainWindow,
    QMenu,
    QToolBar,
)

class WinDockWindow(QDockWidget):
    def __init__(self, title='Window List', parent=None):
        super().__init__(title, parent)
        self.tableNames = []
        if parent and hasattr(parent, 'tableNames'):
            self.tableNames = parent.tableNames

        self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.toggleViewAction().setIcon(QIcon(':/Resources/Images/windows.png'))
        parent and hasattr(parent, 'viewMenu')    and parent.viewMenu.addAction(self.toggleViewAction())
        parent and hasattr(parent, 'fileToolBar') and parent.fileToolBar.addAction(self.toggleViewAction())

        self.listView = QListView(self)
        self.listView.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.listView.setContextMenuPolicy(Qt.CustomContextMenu)
        self.listView.customContextMenuRequested.connect(self.dockWinContextMenuResponse)
        stringListModel = QStringListModel(self.tableNames, self)
        self.listView.setModel(stringListModel)
        self.listView.setEditTriggers(QAbstractItemView.NoEditTriggers)

        self.innerMain = QMainWindow(self)
        self.innerMain.setWindowFlags(Qt.Widget)
        self.toolBar = QToolBar(self.innerMain)
        self.toolBar.setIconSize(QSize(16, 16))
        self.toolBar.addAction(self.toggleViewAction())
        self.innerMain.addToolBar(self.toolBar)
        self.innerMain.setCentralWidget(self.listView)
        self.setWidget(self.innerMain)

    def dockWinContextMenuResponse(self, pos):
        self.contextMenu = QMenu(self)
        self.contextMenu.addAction(QIcon(':/Resources/Images/copy.png'), '&View')
        self.contextMenu.addAction(QIcon(':/Resources/Images/copy.png'), '&Edit')
        self.contextMenu.exec_(QCursor.pos())

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    main = QMainWindow()
    main.dockWin = WinDockWindow(parent=main)
    main.addDockWidget(Qt.LeftDockWidgetArea, main.dockWin)
    main.show()
    sys.exit(app.exec_())
